/*
    see also convert.hs
*/


#include <stdio.h>

int convert(char * file)
{
	FILE * f = fopen(file, "r");
    if (f==0) {
        printf("file open fail: %s\n", file);
        return 0;
    }

	int n=0;
	int x, y, z;
	double v,v0,v1;
	int x0,y0,z0,x1,y1,z1;
	int cnt=0;
	do {
		n = fscanf(f, "%d %d %lf %d", &x, &y, &v, &z);
		if (n<1)
			break;
		if (cnt==0) {
			x0=x1=x;
			y0=y1=y;
			z0=z1=z;
			v0=v1=v;
		} else {
			x0=x0<x?x0:x;
			y0=y0<y?y0:y;
			z0=z0<z?z0:z;
			v0=v0<v?v0:v;
			x1=x1>x?x1:x;
			y1=y1>y?y1:y;
			z1=z1>z?z1:z;
			v1=v1>v?v1:v;
		}
		//if (cnt%10000==0)
		//	printf("%d %d %d %lf\n", x, y, z, v);
		++cnt;
	}while(1);
	fclose(f);
	printf("# CPM INFO VERSION 1.0\n");
	printf("SOURCE %s\n", file);
	printf("COUNT %d\n", cnt);
	printf("MIN %d %d %d %lf\n", x0, y0, z0, v0);
	printf("MAX %d %d %d %lf\n", x1, y1, z1, v1);
	return 1;
}

#if 0
int main(int argc, char ** argv)
{
    if (argc<2) {
        printf("usage:conv <cpm file>\n");
        return 0;
    }
    
    return convert(argv[1])
}
#endif
